<?php
/**
 * Created by PhpStorm.
 * User: ZhangYi
 * Date: 2011-1-14
 * Time: 15:08:54
 * To change this template use File | Settings | File Templates.
 */

class IEasyAnalyzer extends Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8
{

    private $scws;

    private $tokenList;

    private $tokenListPos = 0;

    private $tokenKeys;

    private $initFlag = false;

    function __destruct()
    {
        if ($this->scws) {
            $this->scws->close();
        }
//        echo "destruct\n";
    }

    function __construct()
    {
        parent::__construct();
        self::init();
    }

    private function init()
    {
        $this->scws = scws_new();
        $this->scws->set_charset('utf-8');
        $this->scws->set_dict('/usr/local/scws/etc/dict.utf8.xdb');
        $this->scws->set_rule('/usr/local/scws/etc/rules.ini');
        $this->scws->set_ignore(true);
        $this->scws->set_multi(7);

        $this->initFlag = true;
//        echo "call init" . "\n";
    }

    private function resetScws()
    {

        $this->scws->send_text($this->_input);
        unset($this->tokenList);
        unset($this->tokenKeys) ;
        $this->tokenList = array();
        $this->tokenKeys = array();
        $this->tokenListPos = 0;
        while ($tmp = $this->scws->get_result())
        {
            foreach ($tmp as $word)
            {
                $str = $word['word'];
                if (!in_array($str, $this->tokenKeys)) {
                    $strlen = mb_strlen($str, 'utf-8');
                    $off = 0;
                    while ($off < mb_strlen($this->_input, 'utf-8') &&
                           ($pos = mb_strpos($this->_input, $str, $off, 'utf-8')) !== FALSE) {
                        $endpos = $pos + $strlen;
                        $token = new Zend_Search_Lucene_Analysis_Token($str, $pos, $endpos);
                        $this->tokenList[] = $token;
                        $off += $pos + $strlen;

                    }
                    $this->tokenKeys[] = $str;
                }
                //                echo $str . "\n";
            }
        }
    }


    private function initScws()
    {
        $this->scws = scws_new();
        $this->scws->set_charset('utf-8');
        $this->scws->set_dict('/usr/local/scws/etc/dict.utf8.xdb');
        $this->scws->set_rule('/usr/local/scws/etc/rules.ini');
        $this->scws->set_multi(7);
        $this->scws->set_ignore(true);
        $this->scws->send_text($this->_input);

        $this->tokenList = array();
        $this->tokenKeys = array();

        $this->tokenListPos = 0;
        while ($tmp = $this->scws->get_result())
        {
            foreach ($tmp as $word)
            {
                $str = $word['word'];
                if (!in_array($str, $this->tokenKeys)) {
                    $strlen = mb_strlen($str, 'utf-8');
                    $off = 0;
                    while ($off < mb_strlen($this->_input, 'utf-8') &&
                           ($pos = mb_strpos($this->_input, $str, $off, 'utf-8')) !== FALSE) {
                        $endpos = $pos + $strlen;
                        $token = new Zend_Search_Lucene_Analysis_Token($str, $pos, $endpos);
                        $this->tokenList[] = $token;
                        $off += $pos + $strlen;

                    }
                    $this->tokenKeys[] = $str;
                }
                //                echo $str . "\n";
            }
        }
        //        echo count($this->tokenList);
        $this->scws->close();
        $this->initFlag = true;
    }

    public function reset()
    {
        //        parent::reset();
        if (!$this->initFlag) {
            $this->init();
        }
        $this->resetScws();
    }


    public function nextToken()
    {
        if ($this->_input === null) {
            return null;
        }

        if ($this->initFlag == false) {
            $this->init();
        }

        while ($this->tokenListPos < count($this->tokenList))
        {
            $token = $this->tokenList[$this->tokenListPos++];
            return $token;
        }

    }
//    public function tokenize($data, $encoding = '') {
//        //        return parent::tokenize($data, $encoding);
//        $this->setInput($data, $encoding);
//        $tokenList = array();
//        $so = scws_new();
//        $so->set_charset('utf-8');
//        $so->set_dict('/usr/local/scws/etc/dict.utf8.xdb');
//        $so->set_multi(7);
//
//        $so->send_text($this->_input);
//
//        while ($tmp = $so->get_result())
//        {
//            foreach ($tmp as $word)
//            {
//                $str = $word['word'];
//                $pos = $word['off'];
//                $endpos = $word['off'] + $word['len'];
////                $token = $this->normalize(new Zend_Search_Lucene_Analysis_Token($str, $pos, $endpos));
//                $token = new Zend_Search_Lucene_Analysis_Token($str, $pos, $endpos);
//                $tokenList[] = $token;
//
//                //                print_r($token);
//            }
//        }
//        $so->close();
//
//        return $tokenList;
//    }


}
